var user_id = common.getUrlParam('user_id')
getAllRank()
getUserInfo()
$('.rules>img').on('click', function () {
    $('.mark').show()
    $('.rules-box').show()
})
$('.close>img').on('click', function () {
    $('.mark').hide()
    $('.receive-box').hide()
    $('.rules-box').hide()
})

$('.receive-btn').on('click', function () {
    $('.mark').hide()
    $('.receive-box').hide()
    $('.rules-box').hide()
    window.location.reload()
})

var index = 0
$('.switch>li').on('click', function () {
    $(this).addClass('active').siblings().removeClass('active')
    index = $(this).index()
    $('.ranking-box-item').hide()
    $('.ranking-box-' + index).show()
})
$(document).on('click', '.click_gift', function () {
    let gift_id = $(this).attr('gift_id');
    $('.mark').show()
    $('.receive-box').show()
    common.getRequest({user_id: user_id, gift_id: gift_id}, common.API.clickGift, function (res) {
        if (res.code == 0) {
            let data = res.data;
            $('.rules-content').empty()
            if (data.code == 0) {
                $('.gift_receive-111').empty()
                $('.gift_receive-10').empty()
                getUserInfo()
                $('.rules-content').append('<p>您已成功领取<br/>快去【装扮】里面看看吧~</p>')
            } else {
                $('.rules-content').append('<p>' + data.msg + '</p>')
            }
        }

    })
})


function getUserInfo() {
    common.getRequest({user_id: user_id}, common.API.getUserInfo, function (res) {
        if (res.code == 0) {
            let data = res.data
            let rank_info = '未上榜'
            if (data.user_rank != -1) {
                rank_info = '第' + data.user_rank + '名'
            } else {
                $('.ranking-box-0-num').append('<div>\n' +
                    '                                <p class="ranking-box-0-text">距离上榜还有' + data.diff_score + '个</p>\n' +
                    '                                <img src="static/images/bag.png">\n' +
                    '                            </div>')
            }
            $('.my-bag-number').append(' <div><img src="' + data.user_info.portrait + '"></div>\n' +
                '                        <p>' + data.user_info.nickname + '</p>')
            $('.ranking-box-0-head').append(' <div><img src="' + data.user_info.portrait + '"></div>\n' +
                '                        <p>' + data.user_info.nickname + '</p>')
            $('.num').html(rank_info);
            $('.user_rank').html(rank_info);
            $.each(data.gift_info, function (i, v) {
                // $('.blessing-text').append(' <p>' + v.score + v.name + '</p>')
                $('.bag-box').append(' <div class="bag-item">\n' +
                    '                            <p class="bag-text">' + v.name + '</p>\n' +
                    '                            <p>×' + v.score + '</p>\n' +
                    '                        </div>')
            })
            // $('.blessing-text').append(' <p>' + data.min_score + '福袋</p>')
            $('.fu-num').html(data.min_score)
            let ring_status = '<div class="active">已领取</div>';
            let gift_status = '<div class="active">已领取</div>';
            if (data.is_receive == 0) {
                ring_status = ' <div class="click_gift" gift_id="140">领取</div>'
                gift_status = ' <div class="click_gift" gift_id="126">领取</div>'
            }
            $('.gift_receive-111').append(gift_status)
            $('.gift_receive-10').append(ring_status)
        }
    })
}

function getAllRank() {
    common.getRequest({}, common.API.getRank, function (res) {
        if (res.code == 0) {
            let data = res.data
            let num = 0;
            $.each(data, function (i, v) {
                num = i + 1;
                $('.rank_list').append('<div class="ranking-0-item">\n' +
                    '                        <div class="ranking-0-left">\n' +
                    '                            <p>' + num + '</p>\n' +
                    '                            <div class="ranking-img-0 img-rank-' + i + '">\n' +
                    '                                <img src="' + v.user_info.portrait + '">\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                        <div class="ranking-0-right">\n' +
                    '                            <div class="ranking-0-right-top">\n' +
                    '                                <p class="ranking-name">' + v.user_info.username + '</p>\n' +
                    '                                <p>' + v.min_score + '个<img src="static/images/bag.png"></p>\n' +
                    '                            </div>\n' +
                    '                            <div class="ranking-0-right-bottom">\n' +
                    '                                <p>健康福×' + v.gift_info[133] + '</p>\n' +
                    '                                <p>事业福×' + v.gift_info[134] + '</p>\n' +
                    '                                <p>幸运福×' + v.gift_info[135] + '</p>\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                    </div>')
                if (num < 4) {
                    $('.img-rank-' + i).append('<img class="cat" src="static/images/ranking-bg-' + i + '.png">')
                }
            })
        }
    })
}

function getHowe() {
    common.getRequest({}, common.API.getHowe, function (res) {
        if (res.code == 0) {
            let data = res.data
            let num = 0;
            $.each(data, function (i, v) {
                num = i + 1;
                $('.howe_list').append('<div class="ranking-0-item">\n' +
                    '                        <div class="ranking-0-left">\n' +
                    '                            <p>' + num + '</p>\n' +
                    '                            <div class="ranking-img-0">\n' +
                    '                                <img src="' + v.portrait + '">\n' +
                    '                                <img class="cat" src="static/images/ranking-bg-' + i + '.png">\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                        <div class="ranking-0-right">\n' +
                    '                            <div class="ranking-0-right-top">\n' +
                    '                                <p class="ranking-name">' + v.username + '</p>\n' +
                    '                                <div class="ranking-0-right-bottom">\n' +
                    '                                    <p>ID：' + v.uuid + '</p>\n' +
                    '                                </div>\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                    </div>')


            })
        }
    })

}



